package org.geektimes.projects.user.validator.bean.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

public class IdValidAnnotationValidator implements ConstraintValidator<IdValid,Integer> {


    private String message;

    @Override
    public void initialize(IdValid annotation) {
        this.message = annotation.message();
    }

    @Override
    public boolean isValid(Integer id, ConstraintValidatorContext context) {
        context.disableDefaultConstraintViolation();
        if (Objects.nonNull(id) && id > 0) {
            return true;
        }
        context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
        return false;
    }
}
